<template>
	<view class="group-view">
		<view class="line-view"></view>
		<view class="logo-group-view">
			<image :src="base.JavaimgUrl + '/login/logo.png'"></image>
		</view>
		<!-- <button class="login-btn-view" v-if="!isCheck" @click="goPhone()">
			<text>微信登录</text>
		</button>
		<button class="login-btn-view" v-else open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
			<text>微信登录</text>
		</button> -->
		<button class="login-btn-view" @click="userLogin()">
			<text>微信登录</text>
		</button>
		<text @click="phoneLoginClick()" class="phone-login-view">手机号快捷登录</text>

		<view style="display: flex; margin-top: 52rpx; margin-bottom: 64rpx" class="agreement-view">
			<view @click="checkAgreenClick()" style="display: flex; align-items: center">
				<dx-icons v-if="isCheck" color="#F7CB47" size="30rpx" type="circle-filled" />
				<dx-icons v-else color="#ccc" size="30rpx" type="circle" />
				我已阅读并同意
			</view>
			<view class="agreement-view">
				<text @click="registerPolicyClick()" class="agreement-text">《抖鑫用户使用协议》</text>
				<text @click="privacyPolicyClick()" class="agreement-text">《隐私政策》</text>
			</view>
		</view>
	</view>
</template>
<script setup>
import base from '@/common/baseURL.js';
import {
	ref
} from 'vue';
import api from '@/common/api.js';
import {
	onLoad
} from '@dcloudio/uni-app';
import {
	useUserinfoStore
} from '@/stores/userInfo';
const userInfo_Store = useUserinfoStore();
//是否同意
const isCheck = ref(false);
//微信code
let wxCode = '';
//判断是否新用户
let wxUnionId = '';
let wxOpenId = '';
// const goPhone = () => {
// 	uni.showToast({
// 		title: '请先勾选同意用户协议',
// 		icon: 'none',
// 		duration: 2000
// 	});
// };
const userLogin = async () => {

	console.log("🚀 ~ userLogin ~ duration:", 111)
	if (!isCheck.value) {
		return uni.showToast({
			title: '请先勾选同意用户协议',
			icon: 'none',
			duration: 2000
		});
	}
	// 新用户需要去登录页面注册
	if (wxUnionId) {
		toRegister();
		return;
	}

	uni.showLoading({
		mask: true,
		title: '授权登录中'
	});
	const res = await api.miniLogin_API({
		code: wxCode
	});
	uni.hideLoading();
	if (res.code === 10000) {
		let loinMsgDict = res.data.token;
		//是否新用户
		if (loinMsgDict) {
			userInfo_Store.Login(loinMsgDict);
			userInfo_Store.GetUserInfo().then((res) => {
				if (res.mini_open_id) {

					uni.switchTab({
						url: '/pages/main/main'
					});
				}
			});
		} else {
			let wxResult = res.data.wxResult;
			wxUnionId = wxResult.unionid;
			wxOpenId = wxResult.openid;
			toRegister();
		}
	}
};

//获取手机号授权登录
const getPhoneNumber = (e) => {
	console.log('微信授权登录', e);
	if (!e.detail.hasOwnProperty('encryptedData')) {
		// 没有encryptedData代表用户拒绝授权
		return;
	}
	userLogin();
};
const toRegister = () => {
	console.log('进入注册=======>' + wxUnionId);
	let loginParams = {
		unionId: wxUnionId,
		openId: wxOpenId,
		isRegister: true
	};
	uni.navigateTo({
		url: '/pages/login/login?loginParams=' + JSON.stringify(loginParams)
	});
};
const checkAgreenClick = () => {
	if (isCheck.value) {
		isCheck.value = false;
	} else {
		isCheck.value = true;
	}
};
const registerPolicyClick = () => {
	uni.navigateTo({
		url: '/pages/index/h5Webview?titleStr=用户协议&url=' + encodeURIComponent(JSON.stringify(base
			.registerPolicy))
	});
};
const privacyPolicyClick = () => {
	uni.navigateTo({
		url: '/pages/index/h5Webview?titleStr=隐私政策&url=' + encodeURIComponent(JSON.stringify(base
			.privacyPolicy))
	});
};
const phoneLoginClick = () => {
	uni.navigateTo({
		url: '/pages/login/login'
	})

};

const GetLoginCode = () => {
	uni.login({
		scopes: 'auth_user',
		success: (loginInfo) => {
			console.log('loginInfo:' + JSON.stringify(loginInfo));
			wxCode = loginInfo.code;
		}
	});
};
onLoad(() => {
	GetLoginCode();
});
</script>

<style lang="scss" scoped>
page {
	background-color: #ffffff;
}

.group-view {
	height: 100vh;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
}

.line-view {
	width: 100vw;
	height: 2rpx;
	background: rgba(0, 0, 0, 0.06);
}

.logo-group-view {
	flex: 1;
	display: flex;
	align-items: center;
	justify-content: center;
}

.logo-group-view image {
	width: 210rpx;
	height: 198rpx;
}

.login-btn-view {
	display: flex;
	justify-content: center;
	align-items: center;
	margin-left: 4.3vw;
	margin-right: 4.3vw;
	width: 91.4vw;
	height: 88rpx;
	background: linear-gradient(138deg, #f7cb47 0%, #c2cf51 100%);
	border-radius: 88rpx;
	font-size: 32rpx;
	font-family: PingFangSC-Regular, PingFang SC;
	font-weight: 400;
	color: #ffffff;
	margin-top: 100rpx;
}

.phone-login-view {
	font-size: 24rpx;
	font-family: PingFangSC-Regular, PingFang SC;
	font-weight: 400;
	color: #cccccc;
	line-height: 84rpx;
	margin: 20rpx;
}

.agreement-view {
	font-size: 24rpx;
	font-family: PingFangSC-Regular, PingFang SC;
	font-weight: 400;
	color: #cccccc;
	line-height: 44rpx;
}

.agreement-image {
	width: 26rpx;
	height: 26rpx;
	margin: 9rpx;
}

.agreement-text {
	color: #5271ff;
	background: linear-gradient(135deg, #f7cb47 0%, #c2cf51 100%);
	-webkit-background-clip: text;
	-webkit-text-fill-color: transparent;
}

button::after {
	border: 0;
}
</style>